/**
 * Copyright [2010] Delta Air Lines, Inc. All Rights Reserved Access,
 * Modification, or Use Prohibited Without Express Permission of Delta Air
 * Lines.
 */
package com.delta.rafg.coredata;


/**
 * ClientData.
 *
 * @author Delta
 * @version 1.0
 */
public class ClientData extends AObject {


    /**
     * Holds the value of serialVersionUID property.
     */
    private static final long serialVersionUID = -3239629569218370301L;

   
    /**
     * Holds the value of agentId property.
     */
    private String agentId;

    /**
     * Holds the value of agentDeviceId property.
     */
    private String agentDeviceId;

    /**
     * Holds the value of agentBrowser property.
     */
    private String agentBrowser;

    /**
     * Holds the value of agentCity property.
     */
    private String agentCity;

    /**
     * Holds the value of businessProcess property.
     */
    private String businessProcess;

    /**
     * Holds the value of clientSessionId property.
     */
    private String clientSessionId;

    /**
     * Holds the value of serverSessionKey property.
     */
    private String serverSessionKey;

    // Why do we have Locale as an String instead of java.util.Locale???
    // Good question! Ask GWT...
    /**
     * Holds the value of locale property.
     */
    private String locale;
        
    private String sessionKey;

    /**
     * This is a constructor of ClientData.java
     */
    public ClientData() {
    	super();
    	generateServerSessionKey();
    }

    /**
     * This method sets the value of the <code>agentId</code> property,
     * which is of type <code>String</code>.
     * @param agentId - the new value of <code>agentId</code> property.
     */
    public void setAgentId(String agentId) {

        this.agentId = agentId;
    }

    /**
     * This method sets the value of the <code>agentDeviceId</code> property,
     * which is of type <code>String</code>.
     * @param agentDeviceId - the new value of
     * <code>agentDeviceId</code> property.
     */
    public void setAgentDeviceId(String agentDeviceId) {

        this.agentDeviceId = agentDeviceId;
    }

    /**
     * This method sets the value of the <code>agentBrowser</code> property,
     * which is of type <code>String</code>.
     * @param agentBrowser - the new value of
     * <code>agentBrowser</code> property.
     */
    public void setAgentBrowser(String agentBrowser) {

        this.agentBrowser = agentBrowser;
    }

    /**
     * This method sets the value of the <code>agentCity</code> property,
     * which is of type <code>String</code>.
     * @param agentCity - the new value of <code>agentCity</code> property.
     */
    public void setAgentCity(String agentCity) {

        this.agentCity = agentCity;
    }

    /**
     * This method sets the value of the <code>businessProcess</code> property,
     * which is of type <code>String</code>.
     * @param businessProcess - the new value of
     * <code>businessProcess</code> property.
     */
    public void setBusinessProcess(String businessProcess) {

        this.businessProcess = businessProcess;
        
        replaceServerSessionKey();
    }

    /**
     * This method sets the value of the <code>clientSessionId</code> property,
     * which is of type <code>String</code>.
     * @param clientSessionId - the new value of
     * <code>clientSessionId</code> property.
     */
    public void setClientSessionId(String clientSessionId) {
    
        this.clientSessionId = clientSessionId;
        
        replaceServerSessionKey();
    }

    /**
     * This method sets the value of the <code>serverSessionKey</code> property,
     * which is of type <code>String</code>.
     * @param serverSessionKey - the new value of
     * <code>serverSessionKey</code> property.
     */
    public void setServerSessionKey(String serverSessionKey) {
    
        this.serverSessionKey = serverSessionKey;
    }

    /**
     * Used to construct a request for in subsequent calls. i.e when a business
     * process calls a service.
     * @param pClientData
     */
    public ClientData(final ClientData pClientData) {

        this.agentId = pClientData.getAgentId();
        this.agentDeviceId = pClientData.getAgentDeviceId();
        this.agentBrowser = pClientData.getAgentBrowser();
        this.agentCity = pClientData.getAgentCity();
        this.businessProcess = pClientData.getBusinessProcess();
        this.clientSessionId = pClientData.getClientSessionId();
        this.serverSessionKey = pClientData.getServerSessionKey();
    }

    /**
     * Used to construct a request for the first time. i.e in the initial entry
     * point, usually a business process. Please note that a session key is
     * generated the first time. and propagated all along.
     * @param pAgentId
     * @param pAgentWorkstation
     * @param pAgentBrowser
     * @param pAgentCity
     * @param pBusinessProcess
     * @param pWebSessionId
     */
    public ClientData(final String pAgentId, final String pAgentWorkstation,
        final String pAgentBrowser, final String pAgentCity,
        final String pBusinessProcess, final String pWebSessionId) {

        this.agentId = pAgentId;
        this.agentDeviceId = pAgentWorkstation;
        this.agentBrowser = pAgentBrowser;
        this.agentCity = pAgentCity;
        this.businessProcess = pBusinessProcess;
        this.clientSessionId = pWebSessionId;
        generateServerSessionKey();
    }

    /**
     * This method is used for generate session key.
     */
    protected final void generateServerSessionKey() {

        serverSessionKey = 
            clientSessionId + ":" + businessProcess + ":" 
            + getTimestamp() + ":" + (Math.random() * 10000000);
    }

    /**
     *This method is the access method for the
     * <code>agentBrowser</code> property,
     * which is of type <code>String</code>.
     * @return the current value of the <code>agentBrowser</code>
     * property.
     */
    public final String getAgentBrowser() {

        return agentBrowser;
    }

    /**
     *This method is the access method for the
     * <code>agentCity</code> property,
     * which is of type <code>String</code>.
     * @return the current value of the <code>agentCity</code>
     * property.
     */
    public final String getAgentCity() {

        return agentCity;
    }

    /**
     *This method is the access method for the
     * <code>agentDeviceId</code> property,
     * which is of type <code>String</code>.
     * @return the current value of the <code>agentDeviceId</code>
     * property.
     */
    public final String getAgentDeviceId() {

        return agentDeviceId;
    }

    /**
     *This method is the access method for the
     * <code>agentId</code> property,
     * which is of type <code>String</code>.
     * @return the current value of the <code>agentId</code>
     * property.
     */
    public final String getAgentId() {

        return agentId;
    }

    /**
     *This method is the access method for the
     * <code>businessProcess</code> property,
     * which is of type <code>String</code>.
     * @return the current value of the <code>businessProcess</code>
     * property.
     */
    public final String getBusinessProcess() {

        return businessProcess;
    }

    /**
     *This method is the access method for the
     * <code>clientSessionId</code> property,
     * which is of type <code>String</code>.
     * @return the current value of the <code>clientSessionId</code>
     * property.
     */
    public final String getClientSessionId() {

        return clientSessionId;
    }

    /**
     * This method is the access method for the
     * <code>locale</code> property,
     * which is of type <code>String</code>.
     * @return the current value of the <code>locale</code>
     * property
     */
    public final String getLocale() {

        return locale;
    }

    /**
     * @return the serverSessionKey
     */
    public final String getServerSessionKey() {

        return serverSessionKey;
    }

    /**
     * This method is used for getting current Time Stamp.
     * @return long
     */
    protected long getTimestamp() {

        // return java.util.Calendar.getInstance().getTimeInMillis();
        // in case this class is GWT'd the above statement may have a problem
        return new java.util.Date().getTime();
    }

    /**
     * This method sets the value of the <code>locale</code> property,
     * which is of type <code>String</code>.
     * @param ${param} - the new value of
     *<code>locale</code> property.
     */
    public void setLocale(final String locale) {

        this.locale = locale;
    }
    
	private void replaceServerSessionKey() {

		String strarray[] = this.serverSessionKey.split(":");

		strarray[0] = getClientSessionId();

		strarray[1] = getBusinessProcess();

		
		sessionKey = "";

		for (String str : strarray)
			sessionKey = sessionKey + str + ":"; 
			//buffer.append(str + ":");

		this.serverSessionKey = sessionKey.substring(0, sessionKey.length()-1);
	}
}
